<template>
  <div class="loginhome">
    <!-- 头部 -->
    <div class="loginheader">
      <span
        class="el-icon-caret-left"
        @click="goback"
        style="cursor: pointer"
      ></span>
      <span>播放界面</span>
    </div>
    <div class="divsty">
      <div class="name">{{ newArr[index].name }}</div>
      <div class="imgsty">
        <img :src="newArr[index].al.picUrl" class="picsty" />
      </div>
      <div class="audiosty">
        <span v-for="(item, index) in songurl" :key="index">
          <audio :src="item.url" controls autoplay></audio>
        </span>
        <div class="btnsty">
          <el-button type="primary" @click="previous">上一首</el-button>
          <el-button type="primary" @click="next">下一首</el-button>
          <el-button type="primary" @click="goremark">看评论</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      songurl: "",
      newArr: [],
      index: "",
      id: [],
    };
  },
  // 后执行
  mounted() {
    // 每日推荐
    if (this.isgetdailysongs == 1) {
      this.isgetdailysong();
    }
    // 歌单
    if (this.iscomesonglist == 2) {
      this.getsonglist();
      this.palyall();
    }
  },
  // 先执行
  created() {
    // 从每日推荐来
    var isgetdailysongs = this.$route.query.isgetdailysongs;
    var index = this.$route.query.index;
    this.isgetdailysongs = isgetdailysongs;
    this.index = index;

    // 从歌单来
    var ispalyall = this.$route.query.ispalyall;
    var iscomesonglist = this.$route.query.iscomesonglist;
    this.ispalyall = ispalyall;
    this.iscomesonglist = iscomesonglist;
    var id = localStorage.getItem("id");
    this.id = id;
  },
  methods: {
    // 一、从每日推荐过来则调用每日推荐歌曲列表
    isgetdailysong() {
      this.axios
        .get(`/recommend/songs`)
        .then((res) => {
          this.daliysongslist = res.data.data.dailySongs;
          this.newArr = this.daliysongslist.map(function (item, index) {
            return { id: item.id, name: item.name, al: item.al };
          });
          this.id = this.newArr[this.index].id;
          this.getsongurl();
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 二、从歌单来
    palyall() {
      if (this.ispalyall == 1) {
        this.index = 1;
      }
    },
    getsonglist() {
      // 获取账户信息
      this.axios
        .get(`/playlist/detail?id=${this.id}`)
        .then((res) => {
          this.newArr = res.data.playlist.tracks;
          this.getsongurl();
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 固定功能

    // 获取播放歌曲的url
    getsongurl() {
      this.axios
        .get(`/song/url?id=${this.newArr[this.index].id}`)
        .then((res) => {
          this.songurl = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 看评论
    goremark() {
      this.$router.push({
        path: "/playsongremark",
        query: { id: this.newArr[this.index].id },
      });
    },
    // 上一首
    previous() {
      this.index--;
      this.getsongurl();
    },

    next() {
      this.index++;
      this.getsongurl();
    },

    // 返回上一页
    goback() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.loginheader {
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  color: red;
  border-bottom: red 1px solid;
  height: 30px;
}
.picsty {
  width: 300px;
  height: 300px;
}
.divsty {
  text-align: center;
  margin-top: 10px;
}
.name {
  text-align: center;
  margin-top: 10px;
}
.imgsty {
  text-align: center;
  margin-top: 10px;
}
.audiosty {
  margin-top: 10px;
}
.btnsty {
  margin-top: 10px;
}
</style>
